package edu.osu.cse551;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class ShakeDetection extends Service implements AccelerometerListener {
	

    private static Context CONTEXT;
    private long lastShake = 0;
    private static long TIME_THRESHOLD = 3000000000L;
    
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate();
		// Shake sensor initialization
        if (AccelerometerManager.isSupported()) {
            AccelerometerManager.startListening(this);
        }
        Log.v("SHAKE", "GOT TO ONCREATE");
	}

	@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        CONTEXT = this;

        Log.i("SHAKE", "Received start id " + startId + ": " + intent);
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        if (AccelerometerManager.isSupported()) {
            Log.i("SHAKE", "is Supported");

            AccelerometerManager.startListening(this);

        } else {
            Log.i("SHAKE", "not Supported");

        }
        return START_STICKY;
    }	

	@Override
	public void onAccelerationChanged(float x, float y, float z) {
        //not sure if needed		
	}

	@Override
	public void onShake(float force) {
        Log.i("SHAKE", "onShake");
        long now = System.nanoTime();
        if ((now - lastShake) > TIME_THRESHOLD) {
        	lastShake = now;
        	startActivity(new Intent(ShakeDetection.this, ShakeMenu.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
        }

	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public static Context getContext() {
		return CONTEXT;
	}
	
}